package com.yb.framework.core.utils;

import java.util.List;

public class StringUtil {

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	public static boolean isNull(String str) {
		return str == null || str.length() == 0 || "null".equals(str) || "NULL".equals(str);
	}

	public static boolean isNotNull(String str) {
		return !isNull(str);
	}

	public static boolean isBlank(final CharSequence cs) {
		int strLen;
		if (cs == null || (strLen = cs.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if (Character.isWhitespace(cs.charAt(i)) == false) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNotBlank(final CharSequence cs) {
		return !isBlank(cs);
	}

	/**
	 * 转换字符串第一个字符为小写
	 */
	public static String getStrByLowerFirstChar(String str) {
		try {
			return str.substring(0, 1).toLowerCase() + str.substring(1);
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 转换字符串第一个字符为大写
	 */
	public static String getStrByUpperFirstChar(String str) {
		try {
			return str.substring(0, 1).toUpperCase() + str.substring(1);
		} catch (Exception e) {
			return "";
		}
	}

	// String转换成String类型数组
	public static String[] str2StringArray(String str) {
		if (isEmpty(str)) {
			return null;
		}
		String stringArray[] = str.split(",");
		return stringArray;
	}

	// String转换成Integer类型数组
	public static Integer[] str2IntegerArray(String str) {
		if (isEmpty(str)) {
			return null;
		}
		String array[] = str.split(",");
		Integer[] integerArray = new Integer[array.length];
		for (int i = 0; i < array.length; i++) {
			try {
				integerArray[i] = Integer.parseInt(array[i]);
			} catch (NumberFormatException e) {
				integerArray[i] = null;
				continue;
			}
		}
		return integerArray;
	}

	// String转换成Long类型数组
	public static Long[] str2LongArray(String str) {
		if (isEmpty(str)) {
			return null;
		}
		String array[] = str.split(",");
		Long[] longArray = new Long[array.length];
		for (int i = 0; i < array.length; i++) {
			try {
				longArray[i] = Long.parseLong(array[i]);
			} catch (NumberFormatException e) {
				longArray[i] = null;
				continue;
			}
		}
		return longArray;
	}

	// String数组转换成Long类型数组
	public static Long[] strArray2LongArray(String... strArray) {
		if (strArray == null || strArray.length <= 0) {
			return null;
		}
		Long[] longArray = new Long[strArray.length];
		for (int i = 0; i < strArray.length; i++) {
			try {
				longArray[i] = Long.parseLong(strArray[i]);
			} catch (NumberFormatException e) {
				longArray[i] = null;
				continue;
			}
		}
		return longArray;
	}

	// Long数组转换成String类型数组
	public static String[] longArray2StrArray(Long... longArray) {
		if (longArray == null || longArray.length <= 0) {
			return null;
		}
		String[] strArray = new String[longArray.length];
		for (int i = 0; i < longArray.length; i++) {
			strArray[i] = String.valueOf(longArray[i]);
		}
		return strArray;
	}

	public static String list2String(List<?> list) {
		if (null != list && list.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (Object obj : list) {
				sb.append(obj.toString()).append(",");
			}
			sb.deleteCharAt(sb.length() - 1);
			return sb.toString();
		}
		return null;
	}
}